<?php
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\user as userModel;
use app\common\model\AssistantUserRel as AssistantUserRelModel;
use app\common\model\StudentInfo as StudentInfoModel;
use app\common\model\Common;
use think\Db;
use think\facade\Log;

class Student extends Api
{
    /**
     * 获取转专业学生班级清单
     * @return array
     */
    public function getChangeMajorStudentList()
    {
        $user = new UserModel();
        $userInfo = $user->getUserInfo($this->userId);
        $mobile = $userInfo['mobile'];

        // 如果自己是助手查询辅导员负责的记录 
        $relModel = new AssistantUserRelModel();
        $relInfo = $relModel->where(['assistantUserMobile' => $userInfo['mobile']])->find();
        if($relInfo) {
            $mobile = $relInfo['userMobile'];
        }

        // 查询自己负责的记录
        $model = new Common();
        $list = $model->query("call getChangeMajorStudentList('".$mobile."');");
        
        if(count($list) > 0){
            $result = [
                'status' => true,
                'data'   => $list[0],
                'msg'    => '查询成功'
            ];
        } else {
            $result = [
                'status' => false,
                'data'   => [],
                'msg'    => '无记录'
            ];
        }
  
        return $result;
    }

    /**
     * 获取转专业学生明细
     * @return array
     */
    public function getChangeMajorStudentDetail()
    {
        $user = new UserModel();
        $bjdm  = input('param.bjdm', '');     // 搜索关键字
        $userInfo = $user->getUserInfo($this->userId);
        $mobile = $userInfo['mobile'];

        // 如果自己是助手查询辅导员负责的记录 
        $relModel = new AssistantUserRelModel();
        $relInfo = $relModel->where(['assistantUserMobile' => $userInfo['mobile']])->find();
        if($relInfo) {
            $mobile = $relInfo['userMobile'];
        }

        // 查询自己负责的记录
        $model = new Common();
        $list = $model->query("call getChangeMajorStudentDetail('".$mobile."', '".$bjdm."');");
        
        if(count($list) > 0){
            $result = [
                'status' => true,
                'data'   => $list[0],
                'msg'    => '查询成功'
            ];
        } else {
            $result = [
                'status' => false,
                'data'   => [],
                'msg'    => '无记录'
            ];
        }
  
        return $result;
    }
     
    /**
     * 班级批量确认转专业
     * @return array
     */
    public function changeMajorStudentConfirmList()
    { 
        $result = [
            'status' => true,
            'msg'    => '操作成功',
            'data'   => [],
        ];

        $bjdm_ids  = input('param.bjdm_ids', '');     // 搜索关键字
        $bjdms = explode(',', $bjdm_ids);
       
        $user = new UserModel();
        $userInfo = $user->getUserInfo($this->userId);
        $userId = $this->userId;
        $mobile = $userInfo['mobile'];

        // 如果自己是助手查询辅导员负责的记录 
        $relModel = new AssistantUserRelModel();
        $relInfo = $relModel->where(['assistantUserMobile' => $userInfo['mobile']])->find();
        if($relInfo) {
            $mobile = $relInfo['userMobile'];
            $userId = $user->getUserIdByMobile($mobile);
        }

        // 查询学生清单
        for ($i = 0; $i < count($bjdms); $i++) {
            if ($bjdms[$i]) {
                // 查询自己负责的记录
                $model = new Common();
                $list = $model->query("call getChangeMajorStudentDetail('".$mobile."', '".$bjdms[$i]."');");
        
                // 生成学生清单
                $students = [];
                // 生成学生清单
                if(count($list) > 0){
                    $students = $list[0];
                } 

                Db::startTrans();
                
                $studentInfoModel = new StudentInfoModel();
                $uData=[];
                for ($j = 0; $j < count($students); $j++) {
                        // 增加领取记录
                    $uData[] = array('xh'=>$students[$j]['xh'],'teacherId'=>$userId);
                }
                
                if (!$studentInfoModel->saveAll($uData)) {
                    $result['status'] = false;
                    $result['msg'] = '操作失败，请重试';
                    Db::rollback();
                    return $result;
                }
            
                Db::commit();
            }
        }
        
        return $result;
    }

    /**
     * 个人确认转专业
     * @return array
     */
    public function changeMajorStudentConfirmDetail()
    { 
        $result = [
            'status' => true,
            'msg'    => '操作成功',
            'data'   => [],
        ];

        $xh  = input('param.xh', '');                               // 学号

        $user = new UserModel();
        $userInfo = $user->getUserInfo($this->userId);
        $userId = $this->userId;
        $mobile = $userInfo['mobile'];

        // 如果自己是助手查询辅导员负责的记录 
        $relModel = new AssistantUserRelModel();
        $relInfo = $relModel->where(['assistantUserMobile' => $userInfo['mobile']])->find();
        if($relInfo) {
            $mobile = $relInfo['userMobile'];
            $userId = $user->getUserIdByMobile($mobile);
        }

        $xhList = explode(',', $xh);
        $studentInfoModel = new StudentInfoModel();
        $uData=[];
        for ($i = 0; $i < count($xhList); $i++) {
            $uData[] = array('xh'=>$xhList[$i],'teacherId'=>$userId);
        }
        
        if (!$studentInfoModel->saveAll($uData)) {
            $result['status'] = false;
            $result['msg'] = '操作提交失败，请重试';
            Db::rollback();
            return $result;
        }

        Db::commit();  
        
        return $result;
    }

    /**
     * 更换辅导员
     * @return array
     */
    public function changeTeacher()
    { 
        $result = [
            'status' => true,
            'msg'    => '操作成功',
            'data'   => [],
        ];

        $xh  = input('param.xh', '');                               // 学号
        $teacherId  = input('param.teacherId', '');                 // 辅导员ID
        $studentInfoModel = new StudentInfoModel();

        $res = $studentInfoModel->allowField(true)->save(['teacherId'=>$teacherId],['xh'=>$xh]);
        if (!$res) {
            $result['status'] = false;
            $result['msg'] = '操作提交失败，请重试';
            return $result;
        }
        
        return $result;
    }

    /**
     * 更换辅导员
     * @return array
     */
    public function getWrongStudent()
    { 
        $user = new UserModel();
        $userInfo = $user->getUserInfo($this->userId);
        $mobile = $userInfo['mobile'];

        // 如果自己是助手查询辅导员负责的记录 
        $relModel = new AssistantUserRelModel();
        $relInfo = $relModel->where(['assistantUserMobile' => $userInfo['mobile']])->find();
        if($relInfo) {
            $mobile = $relInfo['userMobile'];
        }

        // 查询自己负责的记录
        $model = new Common();
        $list = $model->query("call programWrongStudentInfo('".$mobile."');");
        
        if(count($list) > 0){
            $result = [
                'status' => true,
                'data'   => $list[0],
                'msg'    => '查询成功'
            ];
        } else {
            $result = [
                'status' => false,
                'data'   => [],
                'msg'    => '无记录'
            ];
        }
  
        return $result;
    }

    /**
     * 更换辅导员
     * @return array
     */
    public function receive()
    { 
        $result = [
            'status' => true,
            'msg'    => '操作成功',
            'data'   => [],
        ];

        $xh  = input('param.xh', '');                               // 学号
        $studentInfoModel = new StudentInfoModel();

        $res = $studentInfoModel->allowField(true)->save(['teacherId'=>$this->userId],['xh'=>$xh]);
        if (!$res) {
            $result['status'] = false;
            $result['msg'] = '操作提交失败，请重试';
            return $result;
        }
         
        return $result;
    }
    
}
